"""This module contains classes and functions related to fractions."""

"""Project Euler Solutions Library

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

def quick_fraction_function(f):
    """Returns a modified version of the function, [f], so that its input is 
    converted to a QuickFraction instance if the input is a number.
    """
    
    def new_function(self, n2):
        return f(self, convert_to_fraction(n2))

    return new_function

def convert_to_fraction(n):
    """Returns [n] if [n] is an instance of QuickFraction, otherwise it 
    converts [n] to a QuickFraction before returning it.
    """
    
    if isinstance(n, QuickFraction):
        return n
    else:
        return QuickFraction(n)

class QuickFraction():    
    """This is a faster fraction implementation. This is mainly achieved
    because this class doesn't automatically simplify the fraction it 
    is representing.
    """
    
    def __init__(self, numerator, denominator = 1):
        """Parameters:
            
            numerator - The numerator of the fraction.
            denominator - The denominator of the fraction, if not specified
                        this defaults to 1.
        """
        
        #If either the numerator or the denominator is an instance 
        #of QuickFraction, then divide them to get the new fraction, 
        #otherwise copy the parameters, as is.
        if isinstance(numerator, QuickFraction) \
            or isinstance(denominator, QuickFraction):
            
            new_fraction = numerator / denominator
            
            self.numerator = new_fraction.numerator
            self.denominator = new_fraction.denominator
        else:
            self.numerator = numerator
            self.denominator = denominator
        
    
    @quick_fraction_function
    def __add__(self, n2):    
        return QuickFraction(
                             self.numerator * n2.denominator \
                                + n2.numerator * self.denominator,
                             self.denominator * n2.denominator
                            )
    
    __radd__ = __add__
    
    @quick_fraction_function
    def __sub__(self, n2):
        return QuickFraction(
                             self.numerator * n2.denominator \
                                - n2.numerator * self.denominator,
                             self.denominator * n2.denominator
                            )
    
    @quick_fraction_function
    def __rsub__(self, n2):    
        return QuickFraction(
                             n2.numerator * self.denominator \
                                - self.numerator * n2.denominator,
                             n2.denominator * self.denominator
                            )
            
    @quick_fraction_function
    def __mul__(self, n2):
        return QuickFraction(
                             self.numerator * n2.numerator,
                             self.denominator * n2.denominator
                            )
    
    __rmul__ = __mul__
    
    @quick_fraction_function
    def __div__(self, n2):
        return QuickFraction(
                             self.numerator * n2.denominator,
                             self.denominator * n2.numerator
                            )
    
    @quick_fraction_function
    def __rdiv__(self, n2):
        return QuickFraction(
                             n2.numerator * self.denominator,
                             n2.denominator * self.numerator
                            )
    
    def __str__(self):
        return '/'.join([str(self.numerator), str(self.denominator)])
    
    def __repr__(self):
        return str(self)
